import 'package:OASys/api/UserApi.dart';
import 'package:OASys/components/MyLoading.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';

class RegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MyRegisterPage(),
    );
  }
}

class MyRegisterPage extends StatefulWidget {
  @override
  _MyRegisterPageState createState() => _MyRegisterPageState();
}

class _MyRegisterPageState extends State<MyRegisterPage> {
  var username = "";
  var password = "";
  bool isShow = true;
  void register() {
    print("正在注册");
    print("用户名：$username");
    print("密码：$password");
    setState(() {
      isShow = !isShow;
    });

    if (username.length == 0 || password.length == 0) {
      setState(() {
        this.isShow = !this.isShow;
      });
      Toast.show("请输入账号密码", context,
          duration: Toast.LENGTH_LONG, gravity: Toast.CENTER);
    } else {
      doRegister();
    }
  }

  void doRegister() {
    print("doRegister");
    UserApi().register(
      username,
      password,
      success: (res) {
        this.setState(() {
          this.isShow = !this.isShow;
        });
        Toast.show(
          "注册成功",
          context,
          duration: Toast.LENGTH_LONG,
          gravity: Toast.CENTER,
        );
        Navigator.of(context).pop();
      },
      error: (error) {
        this.setState(() {
          this.isShow = !this.isShow;
        });
        Toast.show(
          error.message,
          context,
          duration: Toast.LENGTH_LONG,
          gravity: Toast.CENTER,
        );
      },
    );
  }

  void usernameChange(value) {
    username = value;
  }

  void pwdChange(value) {
    password = value;
  }

  void toLogin() {
    print('toLogin');
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          color: Color.fromRGBO(240, 240, 240, 1),
          padding: EdgeInsets.fromLTRB(100, 180, 100, 100),
          child: ListView(
            children: [
              Container(
                padding: EdgeInsets.only(bottom: 50),
                child: Center(
                  child: Text(
                    "OA System",
                    style: TextStyle(
                      fontSize: 25,
                      color: Colors.blue,
                    ),
                  ),
                ),
              ),
              TextField(
                decoration: InputDecoration(
                  hintText: '请输入用户名',
                  icon: Icon(Icons.person),
                ),
                onChanged: usernameChange,
              ),
              TextField(
                decoration: InputDecoration(
                  hintText: '请输入密码',
                  icon: Icon(Icons.lock),
                ),
                obscureText: true,
                onChanged: pwdChange,
              ),
              Container(
                padding: EdgeInsets.fromLTRB(0, 5, 5, 5),
                alignment: Alignment.bottomRight,
                child: Text.rich(
                  TextSpan(
                    children: [
                      TextSpan(text: '已有账号？', style: TextStyle(fontSize: 10)),
                      TextSpan(
                        text: '登录',
                        style: TextStyle(color: Colors.blue, fontSize: 10),
                        recognizer: TapGestureRecognizer()..onTap = toLogin,
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 10),
                child: RaisedButton(
                  onPressed: register,
                  child: Text("注  册"),
                  color: Colors.blue,
                  textColor: Colors.white,
                  disabledTextColor: Colors.red,
                ),
              ),
            ],
          ),
        ),
        Offstage(
          offstage: isShow,
          child: MyLoading(
            loadingText: '提交中',
          ),
        ),
      ],
    );
  }
}
